//
//  BanBu_StarSearchViewController.m
//  BanBu
//
//  Created by banbu3 on 7/26/13.
//
//

#import "BanBu_StarSearchViewController.h"
#import "BanBu_NavButton.h"
#import "UIViewController+Animation.h"

@interface BanBu_StarSearchViewController ()

@end

@implementation BanBu_StarSearchViewController
@synthesize currentIndex;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)initWithSelectIndex:(NSInteger)selectIndex
{
    self = [super init];
    if (self) {
        currentIndex = selectIndex;
    }
    return self;
}
-(void)confirm
{
    [self.delegate confirmSelectStarIndex:currentIndex];
    [self dismissViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    

    
    BanBu_NavButton *rightButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    rightButton.position = ButtonPositionRight;
    rightButton.frame = CGRectMake(320-55, 27, 50, 30);
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
     UIBarButtonItem *listButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
//    [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(3, 2, 2, 2)];
    self.navigationItem.rightBarButtonItem = listButton;
    [listButton release];
    [rightButton release];

    
    
    starArr = [[NSMutableArray alloc] initWithObjects:NSLocalizedString(@"listTitle2", nil),NSLocalizedString(@"starItem", nil),NSLocalizedString(@"starItem1", nil),NSLocalizedString(@"starItem2", nil),NSLocalizedString(@"starItem3", nil),NSLocalizedString(@"starItem4", nil),NSLocalizedString(@"starItem5", nil),NSLocalizedString(@"starItem6", nil),NSLocalizedString(@"starItem7", nil),NSLocalizedString(@"starItem8", nil),NSLocalizedString(@"starItem9", nil),NSLocalizedString(@"starItem10", nil),NSLocalizedString(@"starItem11", nil),nil];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return starArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"qweqaz";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }
    cell.textLabel.text = [starArr objectAtIndex:indexPath.row];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row==currentIndex){
        return;
    }
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:currentIndex
                                                   inSection:0];
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        newCell.textLabel.textColor=[UIColor blueColor];
    }
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        oldCell.textLabel.textColor=[UIColor blackColor];
    }
    currentIndex=indexPath.row;
    [self confirm];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==currentIndex){
        return UITableViewCellAccessoryCheckmark;
    }
    else{
        return UITableViewCellAccessoryNone;
    }
}
@end
